#include "../include/nAryTree.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define TRUE 1
#define FALSE 0
#define MAX_NAME 10
#define MAX_DIR 10
//#include "../include/fileDescriptor.h"

struct dirNode* getNode()
{
struct dirNode *p;
p=(struct dirNode *)malloc(sizeof(struct dirNode));
p->firstChild=NULL;
p->rightSibling=NULL;
return p;
}
struct dirNode* insertNAry(struct fileDescriptor *fd,struct dirNode* root)
{
struct dirNode *p,*parent;
char *ref;
int exitStatus=FALSE;
p=getNode();
if(p==NULL)
{
printf("Sorry NO SUFFICIENT MEMORY");
exit(0);
}
strcpy(p->fileDesc->fileName,fd->fileName);
strcpy(p->fileDesc->fullPath,fd->fullPath);
strcpy(p->fileDesc->fileType,fd->fileType);
p->fileDesc->fileSize=fd->fileSize;
p->fileDesc->locationBlockNo=fd->locationBlockNo;
if(root==NULL)
{
root=p;
exitStatus=TRUE;
}
else
{
parent=root;
while(exitStatus==FALSE)
{
if(strstr(p->fileDesc->fullPath,(parent->fileDesc)->fullPath)==NULL)
{
if(parent->rightSibling==NULL)
{
parent->rightSibling=p;
exitStatus=TRUE;
}
else
parent=parent->rightSibling;
}
else
{
if(parent->firstChild==NULL)
{
parent->firstChild=p;
exitStatus=TRUE;
}
else
{
}
}
parent=parent->firstChild;
}
}
return root;
}
void displayNAry(struct dirNode *root)
{
if(root!=NULL)
{
puts(root->fileDesc->fileName);
displayNAry(root->firstChild);
displayNAry(root->rightSibling);
}
//Pre-order traversal
}
void saveToFile(struct dirNode *root,FILE *fstore)
{
if(root!=NULL)
{
fwrite((root->fileDesc),sizeof(struct fileDescriptor),1,fstore);
saveToFile(root->rightSibling,fstore);
saveToFile(root->firstChild,fstore);
}
}

